package com.yunhe.mybatis.web.user;

import com.yunhe.mybatis.entity.Deparment;
import com.yunhe.mybatis.entity.User;
import com.yunhe.mybatis.service.UserService;
import com.yunhe.mybatis.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user/save")
public class UserSaveServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");
        String uname = req.getParameter("uname");
        String password = req.getParameter("password");
        String name = req.getParameter("name");
        Integer age = Integer .valueOf(req.getParameter("age"));
        Integer sex = Integer .valueOf(req.getParameter("sex"));
        String phone = req.getParameter("phone");
        String emall = req.getParameter("emall");
        Integer deparmentId = Integer .valueOf(req.getParameter("deparment"));
        String juese = req.getParameter("juese");

        Deparment deparment = new Deparment();
        User user = new User();

        user.setUname(uname);
        user.setPassword(password);
        user.setName(name);
        user.setAge(age);
        user.setSex(sex);
        user.setPhone(phone);
        user.setEmall(emall);
        user.setRoule(juese);

        deparment.setId(deparmentId);

        user.setDeparment(deparment);

        userService.save(user);

        req.getRequestDispatcher("/success.jsp").forward(req,resp);


    }
}
